<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Exports\PhotoCatalogExport;
use App\Imports\PhotoCatalogImport;
use App\Services\AttachmentService;
use App\Services\BaseService;
use App\Services\CommonService;
use App\Services\PhotoCatalogService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use PHPUnit\Util\Exception;

class PhotoCatalogController extends BaseController
{
    protected $photoCatalogService;

    public function __construct(PhotoCatalogService $photoCatalogService)
    {
        parent::__construct();
        $this->photoCatalogService = $photoCatalogService;
    }

    /**
     * 照片卷内列表
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/18 14:34
     */
    public function index(Request $request) : JsonResponse
    {
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');
        $keyword         = $request->input('keyword', '');
        $photoArchiveId  = $request->input('photo_archive_id');

        $list = $this->photoCatalogService->getList($partCompanyId, $photoClassifyId, $photoArchiveId, $keyword);

        return $this->success($list);
    }

    /**
     * 问题已解决
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024-09-11 15:05
     */
    public function problemResolved(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $photoClassifyId   = $request->input('photo_classify_id');
        $id                = $request->input('id');

        $photoArchiveInstance = (new BaseService())->getPhotoArchiveInstance($partCompanyId, $photoClassifyId);
        $photoCatalogInstance = $this->photoCatalogService->getPhotoCatalogInstance($partCompanyId, $photoClassifyId);
        $photoCatalog         = $photoCatalogInstance->where('id', $id)->first();
        if (!$photoCatalogInstance->where('id', $id)->update(['problem' => ''])) {
            return $this->error('操作失败！');
        }
        $photoArchiveInstance->where('id', $photoCatalog->photo_archives_id)->update(['is_need_check'=>1]);
        CommonService::addProjectActionLog("问题已解决:{$photoCatalog->problem}", $id, $partCompanyId, $photoClassifyId);

        return $this->success('', '操作成功！');
    }

    /**
     * 添加或编辑
     *
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author: lxk
     * @date: 2024-09-12 9:16
     */
    public function store(Request $request) : JsonResponse
    {
        $params          = $request->all();
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');
        if (!$this->photoCatalogService->addOrUpdate($partCompanyId, $photoClassifyId, $params)) {
            return $this->error('操作失败!');
        }

        return $this->success('', '操作成功！');
    }

    /**
     * 导入卷内目录
     *
     * @author: lxk
     * @date: 2024/3/18 14:37
     */
    public function import(Request $request)
    {
        $this->photoCatalogService->isFileValid($request); //验证文件是否合法
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');

        $importCountKey  = get_rand_str('import_photo_catalog');
        redis()->setex($importCountKey, 300, 0); //预估用时最大5分钟
        Excel::import(new PhotoCatalogImport($partCompanyId, $photoClassifyId, $importCountKey), $request->file('file'));
        $count = redis()->get($importCountKey) ?: 0;
        CommonService::addProjectActionLog("导入照片卷内数据:{$count}条", 0, $partCompanyId, $photoClassifyId);
        if (empty($count)) {
            return $this->error('导入【0】条数据，请下载模板对照！确认单元格所在行是否合并，档案号后面的冒号是否是中文');
        }
        return $this->success('', '成功导入【' . $count . '】条数据');
    }

    /**
     * 导出卷内目录
     * 根据项目选择的卷内模板，执行对应的代码
     */
    public function export(Request $request)
    {
        try {
            return Excel::download(new PhotoCatalogExport(), date('Ymd') . '照片卷内目录.xls');
        }
        catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 【照片档案 卷内】单个或批量都行
     * 软删除，ids可以是以","逗号分割的字符串，也可以是数组形式
     * 删除若干数据后，剩余的数据序号和档案号重新排列，第一个序号为1 从小到大递增
     * @author: lxk
     * @date: 2023/6/3 10:26
     */
    public function destroyMore(Request $request) : JsonResponse
    {
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');
        $photoArchivesId = $request->input('photo_archives_id');
        $ids             = to_array($request->input('ids'));

        if (!$this->photoCatalogService->delData($partCompanyId, $photoClassifyId, $photoArchivesId, $ids)) {
            return $this->error('', '删除失败');
        }

        return $this->success('', '删除成功');
    }

    /**
     * 给单个卷内导入 照片 (若干个)
     * $exifInfo = exif_read_data($file->getRealPath(), 'EXIF', true);
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ApiException
     * @author: lxk
     * @date: 2024-09-12 10:29
     */
    public function addPhoto(Request $request) : JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id');
        $photoClassifyId   = $request->input('photo_classify_id');
        $photoCatalogId    = $request->input('photo_catalog_id');
        $file              = $request->file('file');
        $uploadResult      = (new AttachmentService())->upload($file, 'oss', $partCompanyId, $photoClassifyId, 'photo');
        if (!$this->photoCatalogService->addSomePhoto($partCompanyId, $photoClassifyId, $photoCatalogId, $file, $uploadResult)) {
            return $this->error('添加失败~');
        }

        return $this->success('', '添加成功！');
    }

    /**
     * 点击查看照片,返回多个照片路径数组
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ApiException
     * @author: lxk
     * @date: 2024-09-12 14:43
     */
    public function showPhotos(Request $request) : JsonResponse
    {
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');
        $photoCatalogId  = $request->input('photo_catalog_id');

        $list = $this->photoCatalogService->getPhotoList($partCompanyId, $photoClassifyId, $photoCatalogId);

        return $this->success($list);
    }

    /**
     * 删除 照片
     *
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author: lxk
     * @date: 2024/3/19 10:22
     */
    public function delPhoto(Request $request) : JsonResponse
    {
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');
        $photoId         = $request->input('photo_id');

        $this->photoCatalogService->delPhoto($partCompanyId, $photoClassifyId, $photoId);

        return $this->success('', '删除成功！');
    }


}
